package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ArticleConfigService;
import com.heima.article.service.ArticleHomeService;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.behavior.ApUserBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArticleIsDownListener {
    @Autowired
    private ArticleConfigService articleConfigService;
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        if (StringUtils.isNotBlank(message)){
            Map map = JSON.parseObject(message, Map.class);
            articleConfigService.UpdateByArticleId(map);
        }
    }
    @Autowired
    private ArticleHomeService service;
    @KafkaListener(topics = "read_count")
    public void updateReadCount(String message){
        if (StringUtils.isNotBlank(message)){
            ApUserBehaviorDto apUserBehaviorDto = JSON.parseObject(message, ApUserBehaviorDto.class);
            service.updateReadCount(apUserBehaviorDto);
        }
    }
}
